package com.ericom.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name = "t_order")
public class Order extends BaseObject {
	@OneToOne(mappedBy="order",optional = false, cascade = CascadeType.REFRESH)
	public OrderShippingInfo getShippingInfo() {
		return shippingInfo;
	}

	public void setShippingInfo(OrderShippingInfo shippingInfo) {
		this.shippingInfo = shippingInfo;
	}

	@OneToOne(mappedBy="order",optional = false, cascade = CascadeType.REFRESH)
	public OrderPaymentInfo getPaymentInfo() {
		return paymentInfo;
	}

	public void setPaymentInfo(OrderPaymentInfo paymentInfo) {
		this.paymentInfo = paymentInfo;
	}

	@OneToOne(mappedBy="order",optional = false, cascade = CascadeType.REFRESH)
	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	@OneToOne(mappedBy="order",optional = false, cascade = CascadeType.REFRESH)
	public OrderDeliveryAddress getDeliveryAddress() {
		return deliveryAddress;
	}

	public void setDeliveryAddress(OrderDeliveryAddress deliveryAddress) {
		this.deliveryAddress = deliveryAddress;
	}

	@OneToMany(mappedBy="order",cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=OrderItem.class)
	public Set<OrderItem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(Set<OrderItem> orderItems) {
		this.orderItems = orderItems;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id)
				.append("member", member).append("status", status)
				.append("orderDate", orderDate).toString();
	}

	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof Order))
			return false;
		Order castOther = (Order) other;
		return new EqualsBuilder().append(id, castOther.id)
				.isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id).toHashCode();
	}
	
	@ManyToOne(cascade = { CascadeType.MERGE })  
	@JoinColumn(name = "member_id")	
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	@Column(name="order_date")
	public String getOrderDate() {
		return orderDate;
	}

	
	public void setOrderDate(String orderDate) {
		this.orderDate = orderDate;
	}

	
	private Long id;
	private Member member;
	private String status;
	private String orderDate;
	private Set<OrderItem> orderItems=new HashSet<OrderItem>();
	private OrderDeliveryAddress deliveryAddress;
	private Invoice invoice;
	private OrderPaymentInfo paymentInfo;
	private OrderShippingInfo shippingInfo;

}
